<?php
/*
 * @package Analyser
 * @author Yves Peeters
*/
include_once('Filesize.php');
include_once('CodeValidator.php');
include_once('MetaAnalyser.php');
include_once('SpiderDB.php');
include_once('PageanalyserDB.php');
include_once('advanced_html_dom.php');
include_once('MemoryUsage.php');
/**
 * class with tools to parse pages
 * @todo remove dependancys
 */
class PageAnalyser {
    private $spiderdb;
    private $pagedb;
    private $Ahtmldom;
    private $title;
    private $size;

    public function __construct(){
        $this->spiderdb = new SpiderDB();
        $this->pagedb = new PageAnalyserDB();
        $this->pagedb->initPagaAnalyser();
        $this->Ahtmldom = new advanced_html_dom();
        $this->size = new Size();
    }


    public function analyse(){
        $list = $this->spiderdb->getGoodUrls();
        foreach($list as $item){
            sleep(2);
            $crawler_page = array();
            $crawlid = $this->pagedb->getSpiderIdForUrl($item);
            $crawler_page['crawlid']= $crawlid;

            // W3C
            $w3cobj = new CodeValidator($item);
            $w3cvalid = $w3cobj->check_w3c();
            $crawler_page['w3cvalid']=$w3cvalid;
            unset($w3cobj);
            unset($w3cvalid);

            // perc code vs. content
            $prcCodeContent = $this->size->perc_content($item)."%";
            $crawler_page['codecontentratio']=$prcCodeContent;
            unset($prcCodeContent);

            // html
             
            $htmlstring = $this->Ahtmldom->getHTML($item);
            
            $crawler_page['html']=$htmlstring;
            unset($htmlstring);
            // plain text
            $plaintext = $this->Ahtmldom->getPlainText($item);
            $crawler_page['plaintext']=$plaintext;
            unset($plaintext);
            $this->pagedb->insertPage($crawler_page);
            unset($crawler_page);
            


            //Meta
            $this->Ahtmldom->findMetatags($item);
            $meta['crawlid'] = $crawlid;
            $meta['description'] = $this->Ahtmldom->getDescription();
            $meta['keywords'] =  $this->Ahtmldom->getKeywords();
            $this->pagedb->insertMeta($meta);
            unset($meta);

            //filesize
            $size['crawlid'] = $crawlid;
            $pagesize = $this->size->calculate_page_size($item);
            $size['html'] = $pagesize['page'];
            $size['image'] = $pagesize['img'];
            $size['css'] = $pagesize['css'];
            $size['js'] = $pagesize['js'];
            $size['total'] = $size['html'] + $size['image'] +$size['css']+$size['js'];
            $this->pagedb->insertSize($size);
            unset($pagesize);
            unset($size);
            
            // SEO
            
            $SEO['crawlid']=$crawlid;
            $metaobj = new MetaAnalyser();
            $SEO['suggestedKeywords'] = $metaobj->suggested_keywords($htmlstring);
            $this->pagedb->insertSEO($SEO);
            unset($metaobj);
            unset($SEO);

            unset($crawlid);
            $mem = new  MemoryUsage();
            echo $mem->getUsage();
            echo "\n";
            unset($mem);
        }

    }




}

?>